//
//  MeViewController.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "MeViewController.h"
#import "MeView.h"

@interface MeViewController ()
@property (nonatomic, strong)MeView *meview;

@end

@implementation MeViewController

- (MeView *)meview
{
    if (!_meview)
    {
        if (current_User.isDemoUser.integerValue == 1)
        {
            _meview = [[MeView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-TABBAR_HEIGHT-40*kUIScaleBasePlus)];
        }
        else
        {
            _meview = [[MeView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-TABBAR_HEIGHT)];
        }
        
        WeakSelf(self)
        _meview.reloadDataBlock = ^{
            [weakSelf getUSerInfo];
        };
        
    }
    return _meview;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"我的"];
    
    [self.view addSubview:self.meview];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.viewType = 0;
    
    [self getUSerInfo];
    [self getActivity];
}

- (void)getUSerInfo
{
    
    [infoManager getUserInfo:[NSDictionary dictionary]
                      sucess:^(id  _Nonnull acceptData) {
                          UserInfoModel *model = [[UserInfoModel alloc]initWithDic:acceptData];
                          
                           self.meview.model = model;
                          
                          LCUserDefaultsSet(model.terminalState.stringValue, @"switch");
                          LCUserDefaultsSyn;
                      }
                        fail:^(NSError * _Nullable error) {
                            
                        }];
}


- (void)getActivity
{
    
    NSDictionary *pramaters = @{
                                };

    [infoManager getActivityInfo:pramaters
                      sucess:^(id  _Nonnull acceptData) {
                          [self.meview getActivityInfo:acceptData];
                      }
                        fail:^(NSError * _Nullable error) {
                            
                        }];
}

- (void)reFresToken:(NSNotification *)notfi
{
    
    NSDictionary *pramaters = @{
                                };
    
    if (current_User.hasLogin.integerValue == 1)
    {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshToken" object:nil];
        
        [infoManager getUserInfo:pramaters
                          sucess:^(id  _Nonnull acceptData) {
                              UserInfoModel *model = [[UserInfoModel alloc]initWithDic:acceptData];
                              
                              self.meview.model = model;
                              
                              LCUserDefaultsSet(model.terminalState.stringValue, @"switch");
                              LCUserDefaultsSyn;
                              
                              
                              [infoManager getActivityInfo:pramaters
                                                    sucess:^(id  _Nonnull acceptData) {
                                                        [self.meview getActivityInfo:acceptData];
                                                        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];

                                                        
                                                    }
                                                      fail:^(NSError * _Nullable error) {
                                                          
                                                      }];
                              
                          }
                            fail:^(NSError * _Nullable error) {
                                
                            }];
        
        
    }
    
}
@end
